package ua.org.nuos.sdms.middle.entity;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;

/**
 * Created by IntelliJ IDEA.
 * User: dio
 * Date: 29.01.12
 * Time: 18:50
 * To change this template use File | Settings | File Templates.
 */
@Entity
@Table(name = "email_verification")
@NamedQueries(value = {
        @NamedQuery(name = EmailVerification.QUERY_FIND_BY_CODE, query = "SELECT DISTINCT ur FROM EmailVerification ur WHERE ur.code = :code"),
        @NamedQuery(name = EmailVerification.QUERY_FIND_BY_USER_ID, query = "SELECT DISTINCT ur FROM EmailVerification ur WHERE ur.user.id = :id")
})
public class EmailVerification implements Serializable {

    public static final String QUERY_FIND_BY_CODE = "findEmailVerificationByCode";
    public static final String QUERY_FIND_BY_USER_ID = "findEmailVerificationByUserId";

    @Id
    @NotNull
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id")
    private User user;

    @NotNull
    @Size(max = 255)
    @Column(name = "code", nullable = false, unique = true)
    private String code;

    public EmailVerification() {
    }

    public EmailVerification(User user, String code) {
        this.user = user;
        this.code = code;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        EmailVerification that = (EmailVerification) o;

        if (user != null ? !user.equals(that.user) : that.user != null) return false;
        if (code != null ? !code.equals(that.code) : that.code != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = user != null ? user.hashCode() : 0;
        result = 31 * result + (code != null ? code.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "EmailVerification{" +
                "user=" + user +
                ", code='" + code + '\'' +
                '}';
    }
}
